﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IBll;
using Chinaer.IDao;
using Chinaer.Dao;
using Chinaer.DTO;
using Chinaer.Model;
using System.Transactions;
using Chinaer.Core.Helper;
using Chinaer.Core.BaseCore.Extensions;

namespace Chinaer.Bll
{
    /// <summary>
    /// 注册类
    /// </summary>
    public class Register : Repository, IRegister
    {

        public Chinaer.Model.Common.JsonResult RegisterShangJia(ShangJiaDTO shangjiaDTO)
        {
            Chinaer.Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            if (shangjiaDTO.CheckIsNull().Success == false)//判断实体是否存在
            {
                return shangjiaDTO.CheckIsNull();
            }
            IShangJiaDAO shangjiaDAO = new ShangJiaDAO();
            if (shangjiaDAO.GetByName(shangjiaDTO.LoginName) != null)
            {
                //表示有用户存在 
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                jsonResult.ErrorMessage = "用户已经存在";
                return jsonResult;
            }
            //判断用户名或者密码是否为空
            if (shangjiaDTO.LoginName.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull;
                return jsonResult;
            }
            if (shangjiaDTO.LoginPassword.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull;
                return jsonResult;
            }

            Bll.ShangJiaCaoZuo.Instance.Add(new DTO.ShangJia_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "RegisterShangJia",
                CaoZuoTime = DateTime.Now,
                CreateTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserID = shangjiaDTO.ID,
                UserName = shangjiaDTO.LoginName
            });


            Model.Entities.Model_ShangJia model_shangjia = new Model.Entities.Model_ShangJia();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(shangjiaDTO, model_shangjia);
            if (string.IsNullOrEmpty(model_shangjia.ID) || model_shangjia.ID.Trim() == "")
            {
                model_shangjia.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            model_shangjia.BasePersonInfoID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            model_shangjia.CreateTime = DateTime.Now;
            model_shangjia.IsDelete = false;
            model_shangjia.IsUse = true;
            //默认的角色编号
            model_shangjia.RoleID = Chinaer.Core.Helper.ConfigHelper.GetAppSetting("DefaultRoleID");
            Model.Entities.Model_Base_PersonInfo model_personInfo = new Model.Entities.Model_Base_PersonInfo();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(shangjiaDTO, model_personInfo);
            model_personInfo.ID = model_shangjia.BasePersonInfoID;
            model_personInfo.CreateTime = DateTime.Now;
            model_personInfo.IsDelete = false;
            model_personInfo.IsUse = true;
            model_personInfo.LoginCount = 0;
                        
            try
            {
                using (TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    shangjiaDAO.Add(model_shangjia);
                    IPersonInfoDAO personInfoDAO = new PersonInfoDAO();
                    personInfoDAO.Add(model_personInfo);
                    scope.Complete();
                }
                jsonResult.Success = true;
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;

            }
            return jsonResult;
        }

        public Chinaer.Model.Common.JsonResult RegisterAdministrator(AdministratorDTO administratorDTO)
        {
            Chinaer.Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            //表示实体为空
            if (administratorDTO.CheckIsNull().Success == false)
            {
                return administratorDTO.CheckIsNull();
            }
           
            //判断用户名或者密码是否为空
            if (administratorDTO.LoginName.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull;
                jsonResult.ErrorMessage = jsonResult.LoginStatus.ToDescription();
                return jsonResult;            
            }
            if (administratorDTO.LoginPassword.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull;
                jsonResult.ErrorMessage = jsonResult.LoginStatus.ToDescription();
                return jsonResult;

            }
            IAdministratorDAO administratorDAO = new AdministratorDAO();
            if (administratorDAO.GetByName(administratorDTO.LoginName) != null)
            {
                //表示有用户存在 
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                jsonResult.ErrorMessage = "用户已经存在";
                return jsonResult;
            }

            Bll.AdministratorCaoZuo.Instance.Add(new DTO.Administrator_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "RegisterAdministrator",
                CreateTime = DateTime.Now,
                CaoZuoTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                IsDelete = false,
                IsUse = true,
                UserID = administratorDTO.ID,
                UserName = administratorDTO.LoginName
            });
            Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(administratorDTO, model_Administrator);
            if (string.IsNullOrEmpty(model_Administrator.ID) || model_Administrator.ID.Trim() == "")
            {
                model_Administrator.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            model_Administrator.BasePersonInfoID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            model_Administrator.CreateTime = DateTime.Now;
            model_Administrator.IsDelete = false;
            model_Administrator.IsUse = true;
            //默认的角色编号
            model_Administrator.RoleID = Chinaer.Core.Helper.ConfigHelper.GetAppSetting("DefaultRoleID");
            Model.Entities.Model_Base_PersonInfo model_personInfo = new Model.Entities.Model_Base_PersonInfo();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(administratorDTO, model_personInfo);
            model_personInfo.ID = model_Administrator.BasePersonInfoID;
            model_personInfo.CreateTime = DateTime.Now;
            model_personInfo.IsDelete = false;
            model_personInfo.IsUse = true;
            model_personInfo.LoginCount = 0;
            try
            {
                using (TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    administratorDAO.Add(model_Administrator);
                    IPersonInfoDAO personInfoDAO = new PersonInfoDAO();
                    personInfoDAO.Add(model_personInfo);
                    scope.Complete();
                }
                jsonResult.Success = true;
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);

            }
            return jsonResult;
        }

        public Chinaer.Model.Common.JsonResult RegisterCustomer(CustomerDTO customerDTO)
        {
            Chinaer.Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            //首先检测是否实体为Null
            jsonResult = customerDTO.CheckIsNull();
            if (jsonResult.Success == false)
            {
                return jsonResult;
            }           
            //判断用户名或者密码是否为空
            if (customerDTO.LoginName.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull;
                return jsonResult;
            }
            //判断密码是否为空
            if (customerDTO.LoginPassword.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull;
                return jsonResult;
            }
            ICustomerDAO customerDAO = new CustomerDAO();
            if (customerDAO.GetByName(customerDTO.LoginName) != null)
            {
                //表示有用户存在 
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                jsonResult.ErrorMessage = "用户已经存在";
                return jsonResult;
            }
            Bll.CustomerCaoZuo.Instance.Add(new DTO.Customer_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "RegisterCustomer",
                CaoZuoTime = DateTime.Now,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                CreateTime = DateTime.Now,
                IsDelete = false,
                IsUse = true,
                UserName = customerDTO.LoginName
            });
            Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(customerDTO, model_Customer);
            if (string.IsNullOrEmpty(model_Customer.ID) || model_Customer.ID.Trim() == "")
            {
                model_Customer.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            model_Customer.BasePersonInfoID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            model_Customer.CreateTime = DateTime.Now;
            model_Customer.IsDelete = false;
            model_Customer.IsUse = true;
            //默认的角色编号
            model_Customer.RoleID = Chinaer.Core.Helper.ConfigHelper.GetDefaultCustomerRoleID();
            Model.Entities.Model_Base_PersonInfo model_personInfo = new Model.Entities.Model_Base_PersonInfo();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(customerDTO, model_personInfo);
            model_personInfo.ID = model_Customer.BasePersonInfoID;
            model_personInfo.CreateTime = DateTime.Now;
            model_personInfo.IsDelete = false;
            model_personInfo.IsUse = true;
            model_personInfo.LoginCount = 0;
            try
            {
                using (TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    customerDAO.Add(model_Customer);
                    IPersonInfoDAO personInfoDAO = new PersonInfoDAO();
                    personInfoDAO.Add(model_personInfo);
                    scope.Complete();
                }
                jsonResult.Success = true;
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);

            }
            return jsonResult;
        }

        public Chinaer.Model.Common.JsonResult RegisterEmployee(EmployeeDTO employeeDTO)
        {
            Chinaer.Model.Common.JsonResult jsonResult = new Model.Common.JsonResult();
            //首先检测是否实体为Null
            jsonResult = employeeDTO.CheckIsNull();
            if (jsonResult.Success == false)
            {
                return jsonResult;
            }
            //判断用户名或者密码是否为空
            if (employeeDTO.LoginName.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull;
                return jsonResult;
            }
            //判断密码是否为空
            if (employeeDTO.LoginPassword.CheckIsNull().Success == false)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.BaseCore.LoginStatus.UserNameIsNull.ToDescription();
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.PasswordIsNull;
                return jsonResult;
            }
            IEmployeeDAO employeeDAO = new EmployeeDAO();
            if (employeeDAO.GetByName(employeeDTO.LoginName) != null)
            {
                //表示有用户存在 
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                jsonResult.ErrorMessage = "用户已经存在";
                return jsonResult;
            }
            //添加操作记录信息
            Bll.EmployeeCaoZuo.Instance.Add(new DTO.Employee_CaoZuoDTO()
            {
                CaoZuoFanfFaName = "RegisterEmployee",
                CaoZuoTime = DateTime.Now,
                UserName = employeeDTO.LoginName,
                UserID = employeeDTO.ID,
                IsUse = true,
                IsDelete = false,
                ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID(),
                CreateTime = DateTime.Now
            });
            Model.Entities.Model_Employee model_Employee = new Model.Entities.Model_Employee();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(employeeDTO, model_Employee);
            if (string.IsNullOrEmpty(model_Employee.ID) || model_Employee.ID.Trim() == "")
            {
                model_Employee.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            model_Employee.BasePersonInfoID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            model_Employee.CreateTime = DateTime.Now;
            model_Employee.IsDelete = false;
            model_Employee.IsUse = true;
            //默认的角色编号
            model_Employee.RoleID = Chinaer.Core.Helper.ConfigHelper.GetDefaultEmployeeRoleID();
            Model.Entities.Model_Base_PersonInfo model_personInfo = new Model.Entities.Model_Base_PersonInfo();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(employeeDTO, model_personInfo);
            model_personInfo.ID = model_Employee.BasePersonInfoID;
            model_personInfo.CreateTime = DateTime.Now;
            model_personInfo.IsDelete = false;
            model_personInfo.IsUse = true;
            model_personInfo.LoginCount = 0;
            try
            {
                using (TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    employeeDAO.Add(model_Employee);
                    IPersonInfoDAO personInfoDAO = new PersonInfoDAO();
                    personInfoDAO.Add(model_personInfo);
                    scope.Complete();
                }
                jsonResult.Success = true;
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);

            }
            return jsonResult;
        }
    }
}
